#!/usr/bin/env zsh
set -u -o pipefail

#==============================================================================
# Currently a work in progress.
#
#     Comments to be filled shortly.
#==============================================================================

###############################################################################
# Init
###############################################################################

_USERNAME="${1}"

IFS=$'\n'

#==============================================================================
# BIBLIO_INSTANCE can be set accordingly by the user
#     
#     Additional instances found here:
#
#     https://git.sr.ht/~cadence/bibliogram-docs/tree/master/docs/Instances.md
#
#==============================================================================

BIBLIO_INSTANCE="pussthecat.org"
_page=1

#==============================================================================
# Error table
#==============================================================================

func_err() {
  echo "[ERR.] Failed: $1()"
  exit
}

#
# Internal table
#

#==============================================================================
#
# __get_images():
#
#     Regex gets slightly hairy here, parsing HTML is not always graceful.
#
#     post_images:   Post URLs parsed from the temp file containing page data.
# 
#     post_img_url:  URLs of invididual JPEG post images, constructed after 
#                    parsing HTML from the temp file.
#
# __page_details():
#
#     new_page_flag: Flag set to indicate a following page,
#                    if the detection is not set off, 
#                    the page count (_page) is incremented.
#
#     post_spread:   The number of posts per page.
#
#     post_count:    The number of media per post, for instance; one post
#                    may contain more than one form of media
#
#==============================================================================

__get_images() {
   post_images=($(grep -Eo '/imageproxy[^ ]{1,}' "${_tmp_file}.ind" | grep jpg | tr -d '"' | sed 's/(U|u)serID=[0-9]*&amp;//g;s/><meta//I' ))

   for ((post_img_ct=1 ; post_img_ct <= ${#post_images[@]} ; post_img_ct++)); do
     post_images[${post_img_ct}]=$(sed -E 's/(U|u)serID=[0-9]*&amp;//g;s/><meta//I' <<< ${post_images[${post_img_ct}]}) 
     post_img_url="https://bibliogram.${BIBLIO_INSTANCE}${post_images[${post_img_ct}]}"
     wget $post_img_url -O "${_USER_DIR}/${post_id:3}_${post_img_ct}.jpg" 
   done
  unset post_img_ct post_images
}
     
__page_details() {
  if [[ ! $(grep "No more" ${_tmp_file}) = 1 ]]; then
    new_page_flag=1
    post_spread=($(grep -Eo "/p/[^ ]{11}" ${_tmp_file}))
    post_count=${#post_spread[@]}
    ((_page++))
  else
    new_page_flag=0
  fi
}

#==============================================================================
# General Function Table
#
#     [To be filled]
#
#==============================================================================
     
_browse_posts() {
  for ((page_posts=1 ; page_posts <=${post_count} ; page_posts++)); do
    echo "${page_posts}/${post_count}"
    post_id="${post_spread[${page_posts}]}"
    _post_url="https://bibliogram.${BIBLIO_INSTANCE}${post_id}"
    wget $_post_url -qO ${_tmp_file}.ind
    [ ! -e "${_tmp_file}.ind}" ] && __get_images || func_err $0
  done
  unset post_id _post_url
}

_get_page() {
  _PROFILE_URL="https://bibliogram.${BIBLIO_INSTANCE}/fragment/user/${_USERNAME}/${_page}?type=timeline"
  wget $_PROFILE_URL -qO "${_tmp_file}"
  __page_details ${_tmp_file}
}

_mkdir() {
  _USER_DIR="${HOME}/.igget/${_USERNAME}"
  mkdir -p "${_USER_DIR}"
}

       
_mktmps() {
  LC_CTYPE=C
 
  _tmp_name=$(tr -dc '[:alnum:]' < /dev/urandom | head -c 8)
  
  mkdir -p "${_USER_DIR}/tmp"
  _tmp_file="${_USER_DIR}/tmp/${_tmp_name}"
  touch ${_tmp_file}
  touch ${_tmp_file}.ind
  
  [ ! -e "${_tmp_file}" ] && func_err $0
}

#==============================================================================
 
_mkdir
_mktmps 
_get_page ${_tmp_file}
_browse_posts   
       
for i in {1..5}; do
  _get_page
  _browse_posts
done

